package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gnet.uc.swipe.listview.SwipeMenu;
import com.gnet.uc.swipe.listview.SwipeMenuCreator;
import com.gnet.uc.swipe.listview.SwipeMenuItem;
import com.gnet.uc.swipe.listview.SwipeMenuListView;
import com.yy.common.base.BaseAdapter;
import com.yy.common.commonutils.DisplayUtil;
import com.yy.common.commonutils.FileUtil;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonutils.ImageUtil;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonutils.ToastUitl;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.ProductInfo;
import com.yy.jhl.bean.SealBoxListInfo.DataBean;
import com.yy.jhl.bean.SealBoxListInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.ui.main.activity.PhotosShowActivity;
import com.yy.jhl.ui.manager.activity.SealBoxAddActivity;
import com.yy.jhl.ui.manager.activity.SealBoxAddWebViewActivity;
import com.yy.jhl.util.DataRequestUtil;
import com.yy.jhl.util.DeviceUtil;
import com.yy.jhl.util.DialogUtil;
import com.yy.jhl.util.QRCodeUtil;
import com.yy.jhl.util.ZXingUtils;
import com.yy.jhl.widget.SwipeLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Ellis.Li on 2017/6/15.
 */

public class SealBoxAdapter extends BaseAdapter {
    private List<SealBoxListInfo.DataBean> modelList;
    boolean isEdit = false;
    String batchId = "";

    public void setBatchId(String batchId) {
        this.batchId = batchId;
    }

    public SealBoxAdapter(Context context, boolean isEdit) {
        super(context);
        this.isEdit = isEdit;
        modelList = new ArrayList<>();
    }
    @Override
    public void removeItem(int position) {
        super.removeItem(position);
        modelList.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public void clearData() {
        super.clearData();
        modelList.clear();
        notifyDataSetChanged();
    }
    public void addData(List<SealBoxListInfo.DataBean> monomerModels) {
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    public void addAllData(List<SealBoxListInfo.DataBean> monomerModels) {
        clearData();
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder storeHolder;
        if(view == null){
            storeHolder = new Holder();
            view = layoutInflater.inflate(R.layout.item_seal_box,null);
            storeHolder.initView(view);
            view.setTag(storeHolder);
        }else{
            storeHolder = (Holder)view.getTag();
        }
        if (isEdit){
            storeHolder.box_delete_text.setVisibility(View.VISIBLE);
            storeHolder.box_edit_text.setVisibility(View.VISIBLE);
        }else {
            storeHolder.box_delete_text.setVisibility(View.GONE);
            storeHolder.box_edit_text.setVisibility(View.GONE);
        }
        final SealBoxListInfo.DataBean itemModel = modelList.get(i);
        storeHolder.box_edit_text.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                SealBoxAddWebViewActivity.startAction(mContext,itemModel.getPackage_id(),batchId,1000);
            }
        });
        storeHolder.box_delete_text.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                DialogUtil.showEnsureDialogView(mContext, "确定要删除此箱子吗？", new CommonCallBack() {
                    @Override
                    public void getCallBack() {
                        //删除
                        DataRequestUtil.getInstance(mContext).removePackage(itemModel.getPackage_id(), new CommonCallBack() {
                            @Override
                            public void getCallBack() {
                                removeItem(i);
                                notifyDataSetChanged();
                            }
                        });
                    }
                });
            }
        });
        storeHolder.box_code_text.setText("箱号："+itemModel.getPack_num());
        storeHolder.box_time_text.setText(itemModel.getCreate_time());
        String length = StringUtil.isEmpty(itemModel.getLength()) ? "0" : itemModel.getLength();
        String width =  StringUtil.isEmpty(itemModel.getWidth()) ? "0" : itemModel.getWidth();;
        String height =  StringUtil.isEmpty(itemModel.getHeight()) ? "0" : itemModel.getHeight();;
        String suttle_weight =  StringUtil.isEmpty(itemModel.getSuttle_weight()) ? "0" : itemModel.getSuttle_weight();;
        String rough_weight =  StringUtil.isEmpty(itemModel.getRough_weight()) ? "0" : itemModel.getRough_weight();;
        storeHolder.box_size_text.setText("外包装尺寸：长 "+length+"，宽 "+width+"，高 "+height+"\n重量：毛重"
                                            +rough_weight+"，净重 "+suttle_weight);
        if (StringUtil.isEmpty(itemModel.getQrcode_url())){
            storeHolder.barcode_parent.setVisibility(View.GONE);
        }else {
            storeHolder.barcode_parent.setVisibility(View.VISIBLE);
            final Bitmap bitmap = ZXingUtils.createQRImage(itemModel.getQrcode_url(),250,250);
            storeHolder.barcode_image.setImageBitmap(bitmap);
            storeHolder.barcode_image.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    DialogUtil.showSaveDialogView(mContext, new CommonCallBack() {
                        @Override
                        public void getCallBack() {
                            ZXingUtils.saveBitmap(itemModel.getPackage_id(),bitmap);
                        }
                    });
                }
            });
        }
        List<ProductInfo> products = itemModel.getItem();
        if (products != null && products.size() > 0){
            storeHolder.product_parent.removeAllViews();
            int productSize = products.size();
            for (int index=0;index<productSize;index++){
                SwipeLayout swipeLayout = new SwipeLayout(mContext);
                swipeLayout.setScroll(false);
//                swipeLayout.setScroll(isEdit);
                swipeLayout.setFocusable(true);
                swipeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,DisplayUtil.dip2px(55)));
                View content = LayoutInflater.from(mContext).inflate(R.layout.item_seal_box_view,null);
                View right = LayoutInflater.from(mContext).inflate(R.layout.right_swipe,null);
                swipeLayout.setContentView(content);
                swipeLayout.setRightView(right);
                storeHolder.product_parent.addView(swipeLayout);

                TextView productText = (TextView)content.findViewById(R.id.a_text);
                final TextView modelText = (TextView)content.findViewById(R.id.b_text);
                TextView numText = (TextView)content.findViewById(R.id.c_text);
                TextView unitText = (TextView)content.findViewById(R.id.d_text);
                TextView priceText = (TextView)content.findViewById(R.id.e_text);
                TextView allPriceText = (TextView)content.findViewById(R.id.f_text);
                final ProductInfo item = products.get(index);
                productText.setText(item.getItem_name());
                modelText.setText(item.getModel());
                numText.setText(item.getQuantity());
                unitText.setText(item.getUnit());
                priceText.setText(item.getUnit_price());
                String allPrice = "";
                try {
                    allPrice = StringUtil.getSingleDouble(Double.parseDouble(item.getQuantity())*Double.parseDouble(item.getUnit_price()));
                }catch (Exception e){

                }
                allPriceText.setText(allPrice);
                TextView tv_edit = (TextView)right.findViewById(R.id.tv_edit);
                TextView tv_delete = (TextView)right.findViewById(R.id.tv_delete);
                tv_edit.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        //编辑
                        SealBoxAddActivity.startAction(mContext,itemModel.getPackage_id(),batchId,itemModel.getPack_num(),item);
                        return false;
                    }
                });
                tv_delete.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        //删除
                        DataRequestUtil.getInstance(mContext).removePackage(itemModel.getPackage_id(), new CommonCallBack() {
                            @Override
                            public void getCallBack() {
                                removeItem(i);
                                notifyDataSetChanged();
                            }
                        });
                        return false;
                    }
                });
            }
        }

        return view;
    }
    /**
     *
     */
    class Holder{
        private TextView box_code_text;
        private ImageView barcode_image;
        private TextView box_time_text;
        private TextView box_size_text;
        private TextView box_edit_text;
        private TextView box_delete_text;
        private LinearLayout product_parent;
        private LinearLayout barcode_parent;
        void initView(View view) {
            box_code_text = (TextView) view.findViewById(R.id.box_code_text);
            barcode_image = (ImageView) view.findViewById(R.id.barcode_image);
            box_time_text = (TextView) view.findViewById(R.id.box_time_text);
            box_size_text = (TextView) view.findViewById(R.id.box_size_text);
            box_edit_text = (TextView) view.findViewById(R.id.box_edit_text);
            box_delete_text = (TextView) view.findViewById(R.id.box_delete_text);
            product_parent = (LinearLayout) view.findViewById(R.id.product_parent);
            barcode_parent = (LinearLayout) view.findViewById(R.id.barcode_parent);

        }

    }
}
